"""


"""


# 用户总数统计
from datetime import date, timedelta

from rest_framework.generics import GenericAPIView
from rest_framework.mixins import ListModelMixin
from rest_framework.response import Response
from rest_framework.views import APIView

from goods.models import GoodVisitCount
from users.models import User
from meiduo_admin.serializers import home_page_serializers



class TotalCountView(APIView):
    def get(self, request):

        count = User.objects.count()

        return Response({"count": count})


# 当日新增用户统计
class IncrementCountView(APIView):
    def get(self, request):

        today = date.today()

        count = User.objects.filter(date_joined__gte=today).count()

        return Response({"count": count})


# 日活跃用户统计
class DayLoginCountView(APIView):
    def get(self, request):

        today = date.today()

        count = User.objects.filter(last_login__gte=today).count()

        return Response({"count": count})


# 当日订单统计
class DayOrderCountView(APIView):
    def get(self, request):

        today = date.today()

        count = User.objects.filter(orders__create_time__gte=today).count()

        return Response({"count": count})


# 当月注册曲线图
class MonthIncrementCountView(GenericAPIView):
    """

    [{"count":count, "date": date}, ....]


    """
    def get(self, request):

        today = date.today()

        old_date = today - timedelta(days=29)

        total_list = list()

        for i in range(0, 30):
            count = User.objects.filter(date_joined__gte=old_date,
                                        date_joined__lt=old_date + timedelta(days=1)).count()
            count_dict = {"count": count,
                          "date": old_date}

            total_list.append(count_dict)

            old_date += timedelta(days=1)

        return Response(total_list)


class GoodDayView(GenericAPIView, ListModelMixin):

    def get_queryset(self):
        today = date.today()
        queryset = GoodVisitCount.objects.filter(date=today)
        return queryset

    serializer_class = home_page_serializers.GoodDaySerializer

    def get(self, request):

        return self.list(request)
